package com.atguigu.order.orders.listener;


import com.atguigu.order.orders.entity.OmsOrderEntity;
import com.atguigu.order.orders.service.OmsOrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "order.release.order.queue")
public class OrderReleaseOrderQueues {

    @Autowired
    private OmsOrderService omsOrderService;

    @RabbitHandler
    public void listenOrder(OmsOrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try {
            omsOrderService.listenOrder(orderEntity, channel, message);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
